package com.codemonkey.lq.common.builder;

import java.util.HashMap;
import java.util.Map;

// 本来预想的接口Builder<T>,发现思路很像guava中的Supplier<T>
public final class MapBuilder<TValue> {
	private final Map<String, TValue> underMap = new HashMap<String, TValue>();

	// 不让外界访问
	MapBuilder() {

	}

	public MapBuilder<TValue> append(String key, TValue value) {
		underMap.put(key, value);
		return this;
	}

	public Map<String, TValue> build() {
		return underMap;
		//return Collections.unmodifiableMap(underMap);
	}

	public boolean contains(final Object key) {
		return underMap.containsKey(key);
	}

	/**
	 * @param key  
	 * @param defaultVal  不包含该key时的返回值
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T> T get(final Object key, final T defaultVal) {
		return underMap.containsKey(key) ? defaultVal : (T) underMap.get(key);
	}

	@Override
	public String toString() {
		return underMap.toString();
	}
}
